import moment from "moment";
// 全文表格显示字节转换成M,G,T等单位
export const fileSizeFormat = ({ cellValue }: any) => {
  if (!cellValue) {
    return 0;
  }
  const num = 1024.0; // byte
  if (cellValue < num) {
    return cellValue + "B";
  }
  if (cellValue < Math.pow(num, 2)) {
    return (cellValue / num).toFixed(1) + "K";
  } // kb
  if (cellValue < Math.pow(num, 3)) {
    return (cellValue / Math.pow(num, 2)).toFixed(1) + "M";
  } // M
  if (cellValue < Math.pow(num, 4)) {
    return (cellValue / Math.pow(num, 3)).toFixed(1) + "G";
  } // G
  return (cellValue / Math.pow(num, 4)).toFixed(1) + "T"; // T
};
// 格式化时间
export const timeFormat = ({ cellValue }: any, format = "YYYY-MM-DD") => {
  return cellValue ? moment(cellValue).format(format) : "";
};

export const getCurTime = () => {
  // 获取当前日期和时间
  const now = new Date();

  // 获取年、月、日
  const year = now.getFullYear(); // 年
  const month = String(now.getMonth() + 1).padStart(2, "0"); // 月（0-11，需加1）并补零
  const day = String(now.getDate()).padStart(2, "0"); // 日，并补零

  // 获取时、分、秒
  const hours = String(now.getHours()).padStart(2, "0"); // 时，并补零
  const minutes = String(now.getMinutes()).padStart(2, "0"); // 分，并补零
  const seconds = String(now.getSeconds()).padStart(2, "0"); // 秒，并补零

  // 格式化为字符串
  const date = `${year}-${month}-${day}`; // 年-月-日
  const time = `${hours}:${minutes}:${seconds}`; // 时:分:秒

  return `${date} ${time}`;
};

// 获取时间差 targetTime格式："2024-12-28 15:20:17"
export const timeDifference = (targetTime: string): string => {
  const now: Date = new Date();
  const target: Date = new Date(targetTime);
  const diff: number = Math.floor((now.getTime() - target.getTime()) / 1000); // 时间差（单位：秒）

  if (diff < 60) {
    return "刚刚";
  } else if (diff < 60 * 60) {
    const minutes: number = Math.floor(diff / 60);
    return `${minutes}分钟前`;
  } else if (diff < 60 * 60 * 24) {
    const hours: number = Math.floor(diff / (60 * 60));
    return `${hours}小时前`;
  } else if (diff < 60 * 60 * 24 * 7) {
    const days: number = Math.floor(diff / (60 * 60 * 24));
    return `${days}天前`;
  } else {
    const weeks: number = Math.floor(diff / (60 * 60 * 24 * 7));
    return `${weeks}星期前`;
  }
};
